import Vue from 'vue'
import App from './App.vue'
import router from './router'
import {
  Toast
} from 'vant';

router.beforeEach((to, from, next) => {
  if (to.meta.meedAuth) {
    if (localStorage.getItem('token')) {
      next()
    } else {
      router.replace('/login')
    }
  } else {
    next();
  }
})

// 引入vant组件
import Vant from 'vant';
import 'vant/lib/index.css';
Vue.use(Vant);


// 引入axios库
import axios from 'axios';
Vue.prototype.$axios = axios;

axios.defaults.baseURL = 'http://liangwei.tech:3000';





Vue.config.productionTip = false

new Vue({
  router,
  render: function (h) {
    return h(App)
  }
}).$mount('#app');

axios.interceptors.response.use((res) => {
  const {
    message,
    statusCode
  } = res.data;
  // console.log(res);
  if (statusCode || statusCode == 401) {
    Toast.fail(message);
  }
  return res
});
axios.interceptors.request.use((config) => {
  if (localStorage.getItem('token') && !config.headers.Authorization) {
    config.headers.Authorization = "Bearer " + localStorage.getItem("token")
  }
  return config;
})